package cn.bdqn.service.sys.impl;

import cn.bdqn.dao.sys.SysPhoneDao;
import cn.bdqn.entity.Phone;
import cn.bdqn.service.sys.SysPhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: ycbdqn
 * @Package: cn.bdqn.service.sys.impl
 * @Author: huat
 * @Date: 2020/3/12 17:59
 * @Version: 1.0
 */
@Service
public class SysPhoneServiceImpl implements SysPhoneService {
    @Autowired
    SysPhoneDao sysPhoneDao;
    /**
     * 分页查询电话
     * @param page 起始位置
     * @param limit 页面大小
     * @param phoneNumber 电话
     * @param phoneName 电话归属人
     * @return
     */
    @Override
    public Map<String, Object> getAllPhone(int page, int limit, String phoneNumber, String phoneName) {
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("count",sysPhoneDao.countPhone(phoneNumber, phoneName));
        map.put("data",sysPhoneDao.getAllPhone((page-1)*limit, limit, phoneNumber, phoneName));
        map.put("code",0);
        return map;
    }
    /**
     * 根据id获取手机号
     * @param id 手机id
     * @return
     */
    @Override
    public Phone getPhoneById(int id) {
        return sysPhoneDao.getPhoneById(id);
    }
    /**
     * 修改手机号
     * @param phone
     * @return
     */
    @Override
    @Transactional
    public String updatePhone(Phone phone) {
        Integer result=sysPhoneDao.getPhoneByNumber(phone.getPhoneNumber());
        if(result>0){
            return "2";
        }

        return sysPhoneDao.updatePhone(phone)>0?"1":"0";
    }
    /**
     * 根据id删除手机号
     * @param id
     * @return
     */
    @Override
    public boolean deletePhone(int id) {
        return false;
    }
    /**
     * 添加手机号
     * @param phone
     * @return
     */
    @Override
    public String addPhoneNumber(Phone phone) {
        Integer result=sysPhoneDao.getPhoneByNumber(phone.getPhoneNumber());
        if(result>0){
            return "2";
        }
        return sysPhoneDao.addPhoneNumber(phone)>0?"1":"0";
    }
}
